<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    
  </style>
</head>
<body>
  
</body>
<script>

  // 字符转数值：
  // 严格转换：在字符串中只要有一个字符不能转成数值，就是NaN，必须整个字符串都能转成数值，才转
  console.log( Number( "hello" ) );
  console.log( Number( "NaN" ) );
  console.log( Number( "234a654" ) );
  console.log( Number( "234" ) );
  console.log( Number( "234.654" ) );
  console.log( Number( "-234.654" ) );
  
  // true：1，false：0
  console.log( Number( true ) );
  console.log( Number( false ) );
  
  // null：0  
  console.log( Number( null ) );

  // undefined，花括号对象，函数都是NaN
  console.log( Number( undefined ) );
  console.log( Number( {} ) );
  console.log( Number( {age:18} ) );
  console.log( Number( function(){} ) );
  
  // 空数组：0
  console.log( Number( [] ) );
  // 有且只有一个能直接转成数值的数据，就是该数值，否则为NaN
  console.log( Number( [234] ) );
  console.log( Number( ["234"] ) );
  console.log( Number( ["23a4"] ) );
  console.log( Number( [23, 45] ) );

  console.log("=======")

  // 字符转数值：
  // 非严格转换：从左向右依次检测，能转就转，不能转，停止。第一位就不能转，直接NaN

  // parseInt不识别小数点
  console.log( parseInt( "hello" ) );
  console.log( parseInt( "345" ) );
  console.log( parseInt( "345.678" ) );
  console.log( parseInt( "-345.678" ) );
  console.log( parseInt( "345a678" ) );
  console.log( parseInt( "a345a678" ) );

  console.log("=======")

  // parseFloat能识别小数点
  console.log( parseFloat( "hello" ) );
  console.log( parseFloat( "345" ) );
  console.log( parseFloat( "345.678" ) );
  console.log( parseFloat( "-345.678" ) );
  console.log( parseFloat( "345a678" ) );
  console.log( parseFloat( "a345a678" ) );

  console.log("=======")

  // parseInt主要用来字符转数值，取整
  console.log( parseInt( true ) );
  console.log( parseInt( false ) );
  console.log( parseInt( undefined ) );
  console.log( parseInt( null ) );
  console.log( parseInt( {} ) );
  console.log( parseInt( function(){} ) );
  console.log( parseInt( [] ) );
  console.log( parseInt( ["hello"] ) );
  console.log( parseInt( ["456"] ) );

  console.log("=======")

  // 字符转数值：严格转换
  console.log( Math.round( "hello" ) );
  console.log( Math.round( "345" ) );
  console.log( Math.round( "345.678" ) );
  console.log( Math.round( "-345.678" ) );
  console.log( Math.round( "345a678" ) );
  console.log( Math.round( "a345a678" ) );

  console.log("=======")

  // 基本规则同Number，还可以四舍五入取整
  console.log( Math.round( true ) );
  console.log( Math.round( false ) );
  console.log( Math.round( undefined ) );
  console.log( Math.round( null ) );
  console.log( Math.round( {} ) );
  console.log( Math.round( function(){} ) );
  console.log( Math.round( [] ) );
  console.log( Math.round( ["hello"] ) );
  console.log( Math.round( ["456"] ) );
  console.log( Math.round( [34,45] ) );



</script>
</html>